﻿Imports System.Text.RegularExpressions
Imports System.Text

''' <summary>
''' 字符串辅助类
''' Author  ajayumi
''' Date    2010-010-20
''' </summary>
Public Class StringHelper

    Private Shared m_PerUriReg As Regex = New Regex("(file|ftp|http(s)?):\/\/",
                                                      RegexOptions.Compiled Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)


    ''' <summary>
    ''' 获取一组随机的英文字符串
    ''' </summary>
    ''' <param name="_FileNameLength">要获得的文件名的长度</param>
    ''' <returns>字符串，例如：aDeSeS</returns>
    ''' <remarks></remarks>
    Public Shared Function RandLetterString(Optional ByVal _FileNameLength As Integer = 1) As String
        Dim _Rand As Integer = 0
        Dim _StringBuilder As New Text.StringBuilder()
        Dim _Index As Integer = 0
        Try
            While _Index < _FileNameLength
                Randomize()     '初始化随机种子
                _Rand = CInt(Int((122 - 65 + 1) * Rnd() + 65))   '产生随机数
                While (_Rand = 91) Or (_Rand = 92) Or (_Rand = 93) Or (_Rand = 94) Or (_Rand = 95) Or (_Rand = 96)
                    _Rand = CInt(Int((122 - 65 + 1) * Rnd() + 65))   '产生随机数
                End While
                _StringBuilder.Append(Chr(_Rand).ToString)
                _Index += 1
            End While
        Catch ex As Exception
            Throw ex
        Finally
            _StringBuilder = Nothing
            _Index = Nothing
        End Try

        Return _StringBuilder.ToString.Trim()
    End Function

    ''' <summary>
    ''' 获取当前时间字符串
    ''' </summary>
    ''' <returns>字符串，例如：20070101091005</returns>
    ''' <remarks></remarks>
    Public Shared Function DateTimeString(ByVal format As String) As String
        Dim _Result As String = String.Empty

        Try
            If String.IsNullOrEmpty(format) Then
                _Result = DateTime.Now.ToString("yyyyMMddHHmmssfff")
            Else
                _Result = DateTime.Now.ToString(format)
            End If
        Catch ex As Exception
            Throw ex
        Finally
        End Try

        Return _Result
    End Function

    ''' <summary>
    ''' 获取一个唯一标识符字符串
    ''' </summary>
    ''' <returns>字符串</returns>
    ''' <remarks></remarks>
    Public Shared Function GuidString() As String
        Try
            Return System.Guid.NewGuid.ToString()
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    ''' <summary>
    ''' 过滤文件名中的非法字符
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function FilterInvalidChars(ByVal fileName As String) As String
        If String.IsNullOrEmpty(fileName) Then
            Return String.Empty
        End If

        Dim _Result As String = fileName

        _Result = Regex.Replace(fileName, "(\\|\/|\:|\*|\?|\""|<|>|\||\s)", String.Empty, RegexOptions.Singleline Or RegexOptions.Compiled)

        Return _Result
    End Function

    ''' <summary>
    ''' 获取网页标题
    ''' </summary>
    ''' <param name="htmlCode">网页源码</param>
    ''' <returns></returns>
    Public Shared Function GetHtmlDocTitle(ByVal htmlCode As String) As String
        Dim _Title As String = "No Title"
        Dim _Match As Match = Regex.Match(htmlCode, "<title>(?<title>(.*?))<\/title>", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
        If _Match.Success Then
            _Title = _Match.Groups("title").Value.Trim()
        End If

        Return _Title
    End Function

    ''' <summary>
    ''' 获取安全的Uri对象
    ''' </summary>
    ''' <param name="url"></param>
    ''' <param name="baseUri">基Uri</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetSafeUri(url As String, baseUri As Uri) As Uri
        Dim _Result As Uri = Nothing
        If m_PerUriReg.IsMatch(url) = False Then
            If baseUri Is Nothing Then
                Throw New ArgumentException("默认的Uri对象不能为空。")
            End If

            _Result = New Uri(baseUri, url)
        Else
            _Result = New Uri(url)
        End If

        Return _Result
    End Function

    ''' <summary>
    ''' 去除换行符
    ''' </summary>
    ''' <param name="str">待处理的字符串</param>
    Public Shared Function TrimCrLf(str As String) As String
        Dim _Result As String = String.Empty
        If Not String.IsNullOrEmpty(str) Then
            _Result = str.Replace(vbCr, String.Empty).Replace(vbLf, String.Empty).Replace(vbCrLf, String.Empty)
        End If

        Return _Result
    End Function


End Class
